package cn.shuangliang.contract.web;

import cn.shuangliang.contract.model.Admin;
import cn.shuangliang.contract.service.AdminService;
import cn.shuangliang.contract.util.StrUtil;
import cn.shuangliang.contract.vo.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
public class AdminController {
    private AdminService adminService=new AdminService();
    public AjaxResult login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("username");
        String pass = request.getParameter("pass");
        String code=request.getParameter("vcode");
        if(StrUtil.isBlank(code)||!code.equals(request.getSession().getAttribute("vcode"))){
            //request.setAttribute("msg", "验证码错误！");
            //{}
            //
           // request.getRequestDispatcher("/login.html").forward(request, response);
            return AjaxResult.error(401,"验证码错误！");
        }
        Admin teacher = adminService.login(name, pass);
        if (teacher == null) {
//            request.setAttribute("msg", "错误的用户名或者密码！");
//            request.getRequestDispatcher("/login.html").forward(request, response);
            return AjaxResult.error(401,"错误的用户名或者密码！");
        }
        request.getSession().setAttribute("ADMIN_LOGIN", teacher);
        return AjaxResult.success("登录成功");
    }

//    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String name=request.getParameter("username");
//        String pass = request.getParameter("pass");
//        String code=request.getParameter("vcode");
//        if(StrUtil.isBlank(code)||!code.equals(request.getSession().getAttribute("vcode"))){
//            request.setAttribute("msg", "验证码错误！");
//            request.getRequestDispatcher("/login.html").forward(request, response);
//            return;
//        }
//        Admin teacher = adminService.login(name, pass);
//        if (teacher == null) {
//            request.setAttribute("msg", "错误的用户名或者密码！");
//            request.getRequestDispatcher("/login.html").forward(request, response);
//            return;
//        }
//        request.getSession().setAttribute("ADMIN_LOGIN", teacher);
//        //request.getRequestDispatcher("/xxx.jsp").forward(request,response);
//        response.sendRedirect("admin-index.html");
//    }

    public String logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.getSession().invalidate();
        return "/login.html";
    }
    public String toReset(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        return "/WEB-INF/views/resetpass.html";
    }
    public String reset(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
       String old=request.getParameter("oldPass");
       String new1=StrUtil.trim(request.getParameter("newPass"));
       String new2=request.getParameter("rePass");
       if(new1.equals(new2)==false){
           request.setAttribute("msg","两次输入的密码不一致！");
           return "/WEB-INF/views/resetpass.html";
       }
       Long id=((Admin)(request.getSession().getAttribute("ADMIN_LOGIN"))).getId();
       int r=this.adminService.resetPass(id,new1,old);
        request.setAttribute("msg",r>0?"密码重置成功":"密码重置失败");
        return "/WEB-INF/views/resetpass.html";
    }
}
